package cn.selfiot.iotman.view.console;

import javafx.application.Platform;
import org.fxmisc.richtext.StyleClassedTextArea;

import java.io.OutputStream;
import java.nio.charset.Charset;

/**
 * Console输出流
 *
 * @author hugo
 * @since 2020/8/4
 */
public class Console extends OutputStream {
    private final StyleClassedTextArea console;
    
    public Console(StyleClassedTextArea console) {
        this.console = console;
    }
    
    public void appendText(String valueOf) {
        Platform.runLater(() -> console.appendText(valueOf));
    }
    
    @Override
    public void write(int b) {
        String out = new String(String.valueOf((char) b).getBytes(), Charset.defaultCharset());
        appendText(out);
    }
    
    @Override
    public void write(byte[] b) {
        String out = new String(b, Charset.defaultCharset());
        appendText(out);
    }
}
